<%--
  Created by IntelliJ IDEA.
  User: 23301
  Date: 2020/7/12
  Time: 20:11
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>查询书籍</title>
    <%--引入bootstrap在线CDN--%>
    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="page-header">
                <h1>
                    <smail>书籍列表————显示所有书籍</smail>
                </h1>
            </div>
        </div>
        <div class="row">
            <div class="col-md-4 column">
                <%--toAddBook--%>
                <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a>
            </div>
            <div class="col-md-4 column"></div>
            <div class="col-md-4 column">
                <%--查询书籍--%>
                <form class="form-inline" method="post" action="${pageContext.request.contextPath}/book/queryBookName" style="float: right">
                    <span style="color: red;font-weight: bold">${error}</span>
                    <input type="text" placeholder="请输入要查询的书籍名称" class="form-control" name="bookName">
                    <input type="submit" value="查询" class="btn btn-primary">
                </form>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <table class="table table-hover table-striped">
                <thead>
                <tr>
                    <th>书籍编号</th>
                    <th>书籍名称</th>
                    <th>书籍数量</th>
                    <th>书籍详情</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <%--从数据库中查询出书籍信息并遍历展示--%>
                <c:forEach items="${books}" var="book">
                    <tr id="${book.bookID}">
                        <td>${book.bookID}</td>
                        <td>${book.bookName}</td>
                        <td>${book.bookCounts}</td>
                        <td>${book.detail}</td>
                        <td>
                            <%--
                            rest风格应写为
                            <a href="${pageContext.request.contextPath}/book/toUpdateBook/${book.bookID}">修改</a>
                            --%>
                            <a href="${pageContext.request.contextPath}/book/toUpdateBook?id=${book.bookID}">修改</a>
                            &emsp; | &emsp;
                            <%--普通删除--%>
                            <%--<a href="${pageContext.request.contextPath}/book/delBook/${book.bookID}">删除</a>--%>
                            <%--ajax删除--%>
                            <a onclick="delAjax(${book.bookID})">删除</a>
                        </td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
        </div>
    </div>
</div>
<script src="${pageContext.request.contextPath}/static/js/jquery-3.5.1.min.js"></script>
<script type="text/javascript">
    function delAjax(bookID) {
        $.ajax({
            url:"${pageContext.request.contextPath}/book/delBook",
            data:"bookID="+bookID,
            type:"POST",
            dataType:"text",
            success:function (data) {
                if (data==="true"){
                    $("#"+bookID+"").remove();
                }else {
                    alert("删除失败请检查服务器");
                }
            }
        });
    }
</script>
</body>
</html>
